import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.util.ArrayList;
import javax.swing.*;
/**
 * 
 * @author Vo, Wu and Wyan
 *
 */
public class BattleshipView extends JFrame {

	// Components
	/**
	 * 
	 */
	private JButton start_button = new JButton("Start");
	
	/**
	 * 
	 */
	private JLabel status_indicator = new JLabel("Place your aircraft carrier!");
	
	/**
	 * Button grids for player 1.
	 */
	private ButtonGrid p1_grid = new ButtonGrid();
	
	/**
	 * Button grids for player 2.
	 */
	private ButtonGrid p2_grid = new ButtonGrid();
	
	// Constructor
	/**
	 * Producing button grids for both players
	 * @ensures button grids = #10x10 grids
	 */
	public BattleshipView() {
		JPanel content = new JPanel();
		JPanel grid1 = new JPanel();
		JPanel grid2 = new JPanel();
		JPanel superGrid = new JPanel();
		this.setLayout(new BorderLayout());
		superGrid.setLayout(new GridLayout(1,3));
		this.setBounds(0, 0, 1100, 600);
		content.add(status_indicator);
		content.setPreferredSize(new Dimension(500,50));
		//content.add(start_button, BorderLayout.CENTER);
		//Moved to ButtonGrid constructor
		/*p1_grid.setPreferredSize(new Dimension(400,400));
		p1_grid.setMinimumSize(new Dimension(400,400));
		p1_grid.setMaximumSize(new Dimension(400,400));
		p2_grid.setPreferredSize(new Dimension(400,400));
		p2_grid.setMinimumSize(new Dimension(400,400));
		p2_grid.setMaximumSize(new Dimension(400,400));*/
		grid1.add(p1_grid.frame);
		superGrid.add(grid1);
		grid2.add(p2_grid.frame);
		superGrid.add(grid2);
		add(superGrid, BorderLayout.NORTH);
		add(content, BorderLayout.CENTER);
		
			
		this.setTitle("Battleship");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	
	/**
	 * Show the cell that is chosen by each player.  
	 * @param turn <br />
	 * 			the flag that will be set to true when the cell is chosen by the player.
	 * @return true if the cell is chosen and false otherwise by either player.
	 */
	public String getClickedCell(Boolean turn) {
		String cell = "";
		if (turn) {
			cell = p1_grid.getClickedCell();
		}
		else {
			cell = p2_grid.getClickedCell();
		}
		return cell;
	}
	
	/**
	 * Allowing the button grids to be clicked by each player during his or her turn.
	 * @param turn <br />
	 * 		The flag that will indicate when each player can use his or her grids.
	 * @return true for player 1 and false for player 2 during player 1 turn and the 
	 * 			same is true for player 2.
	 */
	public void enableGrid (Boolean turn) {
		if (turn) {
			p1_grid.enableButtons();
		}
		else {
			p2_grid.enableButtons();
		}
	}
	
	/**
	 * Allowing the button grids to be clicked by each player during his or her turn.
	 * @param turn <br />
	 * 		The flag that will indicate when each player can use his or her grids.
	 * @return true for player 1 and false for player 2 during player 1 turn and the 
	 * 			same is true for player 2.
	 */
	public void disableGrid (Boolean turn) {
		if (turn) {
			p1_grid.disableButtons();
		}
		else {
			p2_grid.disableButtons();
		}
	}
	
	/**
	 * Highlight the end points of cell during ship placement for a player to choose.
	 * @param turn <br />
	 * 			The flag that will indicate each player turn to highlight the cell.
	 * @param endpoints <br />
	 * 			A collection of end points from ship placement for each player
	 */
	public void highlightEndpoints (Boolean turn, ArrayList<String> endpoints) {
		if (turn) {
			for (String cell:endpoints) {
				p1_grid.highlightCell(cell);
			}
		}
		else {
			for (String cell:endpoints) {
				p2_grid.highlightCell(cell);
			}
		}
	}

	/**
	 * Dehighlight the end points of cell during ship placement for a player to choose.
	 * @param turn <br />
	 * 			The flag that will indicate each player turn to dehighlight the cell.
	 * @param endpoints <br />
	 * 			A collection of end points from ship placement for each player.
	 */ 		
	public void dehighlightEndpoints (Boolean turn, ArrayList<String> endpoints) {
		if (turn) {
			for (String cell:endpoints) {
				p1_grid.dehighlightCell(cell);
			}
		}
		else {
			for (String cell:endpoints) {
				p2_grid.dehighlightCell(cell);
			}
		}
	}
	
	/**
	 * Update the grids to appropriate color according to the rule of the game. 
	 * @param turn <br />
	 * 			The flag that will indicate each player turn to dehighlight the cell.
	 * @param endpoints <br />
	 * 			A collection of end points from ship placement for each player.
	 */ 
	public void updateGrid(Boolean turn, String cell, String color) {
		if (turn) {
			System.out.println("Attacked cell: " + cell);
			p2_grid.changeColor(cell, color);
		}
		else {
			p1_grid.changeColor(cell, color);
		}
	}
	
	/**
	 * Update the label according to the status of the game.
	 * @param text <br />
	 * 			message that will print out to update the player during the game.
	 */ 
	public void updateLabel(String text) {
		status_indicator.setText(text);
	}
	
	/**
	 * Adding the action listener.
	 * @param click <br />
	 * 			The click action that occurs for each player.
	 */ 
	public void addActionListener (ActionListener click) {
		p1_grid.addActionListener(click);
		p2_grid.addActionListener(click);
	}
	
	/**
	 * Removing action listener.
	 * @param click <br />
	 * 			The click action is removed from the grid for each player.
	 */ 
	public void removeActionerListener (ActionListener click) {
		p1_grid.removeActionListener(click);
		p2_grid.removeActionListener(click);
	}
	
	
}
